
#include <QGraphicsScene>
#include <QGraphicsView>
#include "viewer.h"

void oneDViewer(double *data, int n, QString string){

    double maxcolor = -1e60, mincolor=1e60;
    int i;
    double nomalizedVal;
    for(i=0; i<n; i++){
        if(data[i]>(maxcolor))
            maxcolor = data[i];
        if(data[i]<mincolor)
            mincolor = data[i];
    }

    QGraphicsScene *scene = new  QGraphicsScene;
    scene->setSceneRect(-n*5, -100, n*10, 200);
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);

    for(i=0; i<n; i++){
        if(data[i] > 0)
            nomalizedVal = (data[i])/(maxcolor);
        else
            nomalizedVal = (data[i])/(mincolor);
        if(data[i] == 0)
            scene->addRect(-n*5+i*10,-50,9,50,QPen(QColor::fromHsvF(0.0,1.0,0.0)),QBrush(QColor::fromHsvF(0.0,0.0,1.0),Qt::SolidPattern));
        else if(data[i] > 0)
            scene->addRect(-n*5+i*10,-50,9,50,QPen(QColor::fromHsvF(0.72-nomalizedVal*0.08,1.0*nomalizedVal,1.0)),QBrush(QColor::fromHsvF(0.72-nomalizedVal*0.08,1.0*nomalizedVal,1.0),Qt::SolidPattern));
        else
            scene->addRect(-n*5+i*10,-50,9,50,QPen(QColor::fromHsvF(0.72+nomalizedVal*0.08,1.0*nomalizedVal,1.0)),QBrush(QColor::fromHsvF(0.72+nomalizedVal*0.08,1.0*nomalizedVal,1.0),Qt::SolidPattern));
    }
    QGraphicsView *view = new QGraphicsView(scene);
    view->setRenderHint(QPainter::Antialiasing);
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setDragMode(QGraphicsView::ScrollHandDrag);
    view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, string));
    view->resize(600, 200);
    view->show();
}



void twoDViewer(double *data, int m, int n, QString str){
    double maxcolor = 1e-60, mincolor=1e60;
    double nomalizedVal;
    int i, j;
    for(i=0; i<m*n; i++){
        if(data[i]>maxcolor)
            maxcolor = data[i];
        if(data[i]<mincolor)
            mincolor = data[i];
    }

    QGraphicsScene *scene = new  QGraphicsScene;
    scene->setSceneRect( -n*5,-m*5, n*10+20, m*10+20);
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);

    for(j=0; j<n; j++)
        for(i=0; i<m; i++){
            if(data[j*m+i] > 0)
                nomalizedVal = (data[j*m+i])/(maxcolor);
            else
                nomalizedVal = (data[j*m+i])/(mincolor);

            if(data[j*m+i] == 0)
                scene->addRect(-n*5+j*10,-m*5+i*10,9,9,QPen(QColor::fromHsvF(0.0,1.0,0.0)),QBrush(QColor::fromHsvF(0.0,0.0,1.0),Qt::SolidPattern));
            else if(data[j*m+i] > 0)
                scene->addRect(-n*5+j*10,-m*5+i*10,9,9,QPen(QColor::fromHsvF(0.72-nomalizedVal*0.08,1.0*nomalizedVal,1.0)),QBrush(QColor::fromHsvF(0.72-nomalizedVal*0.08,1.0*nomalizedVal,1.0),Qt::SolidPattern));
            else
                scene->addRect(-n*5+j*10,-m*5+i*10,9,9,QPen(QColor::fromHsvF(0.72+nomalizedVal*0.08,1.0*nomalizedVal,1.0)),QBrush(QColor::fromHsvF(0.72+nomalizedVal*0.08,1.0*nomalizedVal,1.0),Qt::SolidPattern));

           //scene->addRect(-n*5+j*10,-m*5+i*10,10,10,QPen(QColor::fromHsvF(0.15*(data[j*m+i]-mincolor)/(maxcolor-mincolor),1.0,1.0)),QBrush(QColor::fromHsvF(0.15*(data[j*m+i]-mincolor)/(maxcolor-mincolor),1.0,1.0),Qt::SolidPattern));
        }
    QGraphicsView *view = new QGraphicsView(scene);
    view->setRenderHint(QPainter::Antialiasing);
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    view->setDragMode(QGraphicsView::ScrollHandDrag);
    view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, str));
    view->resize(600, 400);
    view->show();
}
